/**
 * Copyright (c) 2016-2019  All rights reserved.
 *
 * https://www.yuncheng.io
 *
 * 版权所有，侵权必究！
 */

package io.yuncheng.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.yuncheng.common.exception.RRException;
import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.R;
import io.yuncheng.common.utils.SysDictConstant;
import io.yuncheng.common.validator.ValidatorUtils;
import io.yuncheng.modules.sys.entity.SysDictEntity;
import io.yuncheng.modules.sys.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据字典
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@Api(tags="字典管理")
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;



    String ARITH = "arith";
    String APP_LEVEL = "app_level";
    String APP_TYPE = "app_type_id";


    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDictService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @ApiOperation("枚举值列表")
    @PostMapping("/list-map")
    public R list(@RequestBody SysDictEntity entity){
        QueryWrapper<SysDictEntity> sub = new QueryWrapper<>();
        sub.setEntity(entity);
        List<SysDictEntity> list = sysDictService.list(sub);
        List<SysDictEntity> collect = list.stream().filter(a -> !a.getType().equals(SysDictConstant.ARITH) ||(( a.getType().equals(SysDictConstant.ARITH) && a.getRemark().equals("1")))).collect(Collectors.toList());
        Map<@NotBlank(message = "字典类型不能为空") String, List<SysDictEntity>> map = collect.stream().collect(Collectors.groupingBy(SysDictEntity::getType));

        return R.ok().put("data", map);
    }
    /**
     * 列表
     */
    @ApiOperation("枚举值列表")
    @PostMapping("/list-map-confirm")
    public R listConfirm(@RequestBody SysDictEntity entity){
        QueryWrapper<SysDictEntity> sub = new QueryWrapper<>();
        sub.setEntity(entity);
        List<SysDictEntity> list = sysDictService.list(sub);
        Map<@NotBlank(message = "字典类型不能为空") String, List<SysDictEntity>> map = list.stream().collect(Collectors.groupingBy(SysDictEntity::getType));

        return R.ok().put("data", map);
    }
    /**
     * 列表
     */
    @ApiOperation("算法列表")
    @GetMapping("/arith-list")
    public R arithList(@RequestParam Map<String, Object> params){
        params.put("type",ARITH);
        PageUtils page = sysDictService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @ApiOperation("算法列表")
    @GetMapping("/app-level-list")
    public R applevel(@RequestParam Map<String, Object> params){
        params.put("type",APP_LEVEL);
        PageUtils page = sysDictService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @ApiOperation("第三方系统分类列表")
    @GetMapping("/part-list")
    public R partList(@RequestParam Map<String, Object> params){
        params.put("type",APP_TYPE);
        PageUtils page = sysDictService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public R info(@PathVariable("id") Long id){
        SysDictEntity dict = sysDictService.getById(id);

        return R.ok().put("dict", dict);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.save(dict);

        return R.ok();
    }
    @ApiOperation("算法保存")
    @PostMapping("/arith-save")
    @RequiresPermissions("sys:dict:save")
    public R arithSave(@RequestBody SysDictEntity dict){
        dict.setType(ARITH);
        dict.setRemark(Constant.StatusType.NORMAL.getValue()+"");
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.save(dict);

        return R.ok();
    }
    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.updateById(dict);

        return R.ok();
    }

    /**
     * 注销
     */
    @PostMapping("/write-off")
    @ApiOperation("注销")
    public R updateOff(Long id){
        SysDictEntity byId = sysDictService.getById(id);
        if(byId == null){
            throw new RRException("无对应id");
        }
        byId.setRemark(String.valueOf(Constant.StatusType.OFF.getValue()));
        sysDictService.updateById(byId);
        return R.ok();
    }

    /**
     * 注销
     */
    @PostMapping("/write-on")
    @ApiOperation("启用")
    public R updateOn(Long id){
        SysDictEntity byId = sysDictService.getById(id);
        if(byId == null){
            throw new RRException("无对应id");
        }
        byId.setRemark(String.valueOf(Constant.StatusType.NORMAL.getValue()));
        sysDictService.updateById(byId);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody Long[] ids){
        sysDictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
